package com.base.library.widget

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.view.View.OnTouchListener
import androidx.recyclerview.widget.RecyclerView

@SuppressLint("ClickableViewAccessibility")
class ParentTouchRecyclerView constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int
) : RecyclerView(context, attrs, defStyleAttr) {
    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)

    init {
        setOnTouchListener(OnTouchListener { v, event -> parentTouch(v, event) })
    }

    /**
     * 找到父容器去处理点击
     */
    private fun parentTouch(view: View?, event: MotionEvent): Boolean {
        if (view == null) {
            return false
        }
        val parent = view.parent as? View?
        if (parent?.isClickable == true) {
            return parent.onTouchEvent(event)
        }
        return parentTouch(parent, event)
    }
}